package com.jin.lazytool.customevent;

import com.jin.lazytool.componment.NumberField;
import com.jin.lazytool.entity.IEvent;
import com.jin.lazytool.entity.MouseClickEvent;
import com.jin.lazytool.utils.JinUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.GridPane;

import java.awt.event.InputEvent;
import java.util.Arrays;
import java.util.List;

public abstract class CustomMouseClickEvent extends CustomEvent{


    private final ChoiceBox<String> buttonStr = new ChoiceBox<>();


    private final NumberField x = new NumberField();
    private final NumberField y = new NumberField();

    private MouseClickEvent event;

    {
        List<String> strings = Arrays.asList("左键", "中键", "右键");
        ObservableList<String> buttons = FXCollections.observableList(strings);
        buttonStr.setItems(buttons);
        buttonStr.setValue(strings.get(0));
        Tooltip.install(luaButton, new Tooltip("""
                返回对象
                {
                    exeFlag: true, // 是否执行        必须返回
                    time: 0,       // 执行前等待时间    必须返回
                    x: 0,          // 鼠标坐标x
                    y: 0           // 鼠标坐标y
                }"""));
    }

    @Override
    public void initAddPanel(GridPane grid) {
        grid.addRow(2, new Label("按键"), buttonStr);
        grid.addRow(3, new Label("位置x"), x);
        grid.addRow(4, new Label("位置y"), y);
        grid.addRow(5, new Label("等待时间"), time);
        grid.addRow(6, luaButton, luaScript);
    }

    @Override
    public void initEditPanel(GridPane grid, IEvent event) {
        this.event = (MouseClickEvent)event;
        buttonStr.setValue(getMouseButtonStr(this.event.getButton()));
        x.setValue(this.event.getX());
        y.setValue(this.event.getY());
        time.setValue(this.event.getTime());
        luaScript.setText(this.event.getLua());
        initAddPanel(grid);
    }

    @Override
    public IEvent fullAddIEvent() {
        event = newEvent();
        event.setTime(time.getNullDefaultValue());
        event.setButton(getMouseButtonInt(buttonStr.getValue()));
        event.setX(x.getValue());
        event.setY(y.getValue());
        event.setLua(luaScript.getText());
        return event;
    }

    @Override
    public void fullEditIEvent() {
        event.setTime(time.getNullDefaultValue());
        event.setButton(getMouseButtonInt(buttonStr.getValue()));
        event.setX(x.getValue());
        event.setY(y.getValue());
        event.setLua(luaScript.getText());
    }

    protected int getMouseButtonInt(String name){
        if("左键".equals(name)){
            return InputEvent.BUTTON1_DOWN_MASK;
        }else if("右键".equals(name)){
            return InputEvent.BUTTON2_DOWN_MASK;
        }else if("中键".equals(name)){
            return InputEvent.BUTTON3_DOWN_MASK;
        }
        JinUtils.error("鼠标未知按键");
        return -1;
    }
    protected String getMouseButtonStr(int button){
        if(InputEvent.BUTTON1_DOWN_MASK == button){
            return "左键";
        }else if(InputEvent.BUTTON2_DOWN_MASK == button){
            return "右键";
        }else if(InputEvent.BUTTON3_DOWN_MASK == button){
            return "中键";
        }
        JinUtils.error("鼠标未知按键");
        return "";
    }

    abstract MouseClickEvent newEvent();
}
